/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ventana.java
 *
 * Created on 07/04/2014, 14:31:34
 */
package TP3;

import javax.swing.JOptionPane;
import TP2.*;

/**
 *
 * @author augusto
 */
public class Ventana extends javax.swing.JFrame {

    int intervalos, muestra, indice;
    String mensaje;
    float media, desviacion;
    private float lambda;

    /** Creates new form Ventana */
    public Ventana() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        cmb_indice = new javax.swing.JComboBox();
        txt_muestra = new javax.swing.JTextField();
        txt_intervalos = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txt_media = new javax.swing.JTextField();
        txt_desviacion = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txt_lambda = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        txt_extremoInf = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txt_ExtremoSup = new javax.swing.JTextField();

        jButton1.setText("Graficar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(94, 94, 94)
                .addComponent(jButton1)
                .addGap(25, 25, 25))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(194, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        cmb_indice.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Uniforme", "Normal", "Exponencial Negativa", "Poisson" }));
        cmb_indice.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmb_indiceActionPerformed(evt);
            }
        });

        jLabel1.setText("Números generados:");

        jLabel2.setText("Cantidad de Intervalos:");

        jLabel3.setText("Media:");

        jLabel4.setText("Distribución:");

        txt_media.setEnabled(false);

        txt_desviacion.setEnabled(false);

        jLabel5.setText("desviación:");

        txt_lambda.setEnabled(false);
        txt_lambda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_lambdaActionPerformed(evt);
            }
        });
        txt_lambda.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txt_lambdaKeyTyped(evt);
            }
        });

        jLabel6.setText("lamda:");

        jButton2.setText("Generar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel7.setText("Extremo superior");

        jLabel8.setText("Extremo inferior");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txt_intervalos, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                            .addComponent(txt_muestra, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                        .addGap(238, 238, 238))
                    .addComponent(cmb_indice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txt_ExtremoSup, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(txt_extremoInf, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))
                .addGap(238, 238, 238))
            .addGroup(layout.createSequentialGroup()
                .addGap(98, 98, 98)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_media, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txt_desviacion, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jButton2)
                        .addComponent(txt_lambda, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(328, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmb_indice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_muestra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_intervalos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_extremoInf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_ExtremoSup, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txt_media, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_desviacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_lambda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jLabel3)))
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
}//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (validarForm()) {
            switch (indice) {
                case 0:
                    Histograma.generarHistograma("Distribución Uniforme", intervalos, generarNumerosUniforme());
                    break;
                case 1:
                    Histograma.generarHistograma("Distribución Uniforme", intervalos, generarNumerosNormal());
                    break;
                case 2:
                    Histograma.generarHistograma("Distribución Normal", intervalos, generarNumerosExponencial());
                    break;
                case 3:
                    Histograma.generarHistograma("Distribución Exponencial", intervalos, generarDistribucionPoisson());
                    break;
                default:
                    JOptionPane.showMessageDialog(this, "Ingresar solo una opción valida");
                    break;
            }
        } else {
            MostrarMensaje(mensaje);
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    private void cmb_indiceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmb_indiceActionPerformed
        bloquearCasillas();
        
        if(cmb_indice.getSelectedIndex() == 0)
        { txt_ExtremoSup.setEnabled(true);
          txt_extremoInf.setEnabled(true);
        }
        
        if (cmb_indice.getSelectedIndex() == 1) {
            txt_desviacion.setEnabled(true);
            txt_media.setEnabled(true);
        }

        if (cmb_indice.getSelectedIndex() == 3) {
            txt_lambda.setEnabled(true);
        }

        if (cmb_indice.getSelectedIndex() == 2) {
            txt_lambda.setEnabled(false);
            txt_desviacion.setEnabled(false);
            txt_media.setEnabled(true);

            txt_extremoInf.setEnabled(false);
            txt_ExtremoSup.setEnabled(false);
        }
        
       


    }//GEN-LAST:event_cmb_indiceActionPerformed

    private void txt_lambdaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_lambdaActionPerformed
    }//GEN-LAST:event_txt_lambdaActionPerformed

    private void txt_lambdaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txt_lambdaKeyTyped
    }//GEN-LAST:event_txt_lambdaKeyTyped

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ventana().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmb_indice;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txt_ExtremoSup;
    private javax.swing.JTextField txt_desviacion;
    private javax.swing.JTextField txt_extremoInf;
    private javax.swing.JTextField txt_intervalos;
    private javax.swing.JTextField txt_lambda;
    private javax.swing.JTextField txt_media;
    private javax.swing.JTextField txt_muestra;
    // End of variables declaration//GEN-END:variables

    private boolean validarForm() {
        boolean validado = true;

        try {
            intervalos = Integer.parseInt(txt_intervalos.getText());
        } catch (NumberFormatException ex) {
            validado = false;
        }
        try {

            muestra = Integer.parseInt(txt_muestra.getText());
        } catch (NumberFormatException ex) {
            validado = false;
        }

        try {

            indice = cmb_indice.getSelectedIndex();
        } catch (IndexOutOfBoundsException ex) {
            validado = false;
        }

        if (indice == 1) {
            try {

                media = Float.parseFloat(txt_media.getText());
            } catch (IndexOutOfBoundsException ex) {
                validado = false;
            }
            try {

                desviacion = Float.parseFloat(txt_desviacion.getText());
            } catch (NumberFormatException ex) {
                validado = false;
            }



        }



        if (indice == 2) {
            try {
                media = Float.parseFloat(txt_media.getText());


            } catch (NumberFormatException ex) {
                validado = false;
            }

        }


        if (indice == 3) {
            try {
                lambda = Float.parseFloat(txt_lambda.getText());
                media = 1 / lambda;
            } catch (NumberFormatException ex) {
                validado = false;
            }

        }

        if (intervalos < 1) {
            validado = false;
            mensaje = "el numero de intervalos tiene que ser mayor a 0. ";
        }
        if (muestra < 1) {
            validado = false;
            mensaje = "el numero de muestra tiene que ser mayor a 0. ";
        }



        return validado;
    }

    private void MostrarMensaje(String mensaje) {
        JOptionPane.showMessageDialog(null, mensaje);
        mensaje = "";
    }

    private void bloquearCasillas() {

        txt_desviacion.setEnabled(false);
        txt_media.setEnabled(false);
        txt_lambda.setEnabled(false);
        txt_ExtremoSup.setEnabled(false);
        txt_extremoInf.setEnabled(false);

    }

    private double[] generarNumerosAleatorios() {

        double vector[] = new double[muestra];
        for (int i = 0; i < muestra; i++) {
            vector[i] = Math.random();
        }
        return vector;
    }

    private double[] generarNumerosUniforme() {

        GeneradorDeDistribuciones g = new GeneradorDeDistribuciones();
        double vector[] = new double[muestra];
        for (int i = 0; i < muestra; i++) {
            vector[i] = g.generarUniforme(Math.random(), Double.parseDouble(txt_extremoInf.getText()), Double.parseDouble(txt_ExtremoSup.getText()));
        }
        return vector;
    }

    private double[] generarNumerosNormal() {
        GeneradorDeDistribuciones g = new GeneradorDeDistribuciones();
        double vector[] = new double[muestra];
        for (int i = 0; i < muestra; i++) {
            vector[i] = g.generarNormal(media, desviacion);
        }
        return vector;
    }

    private double[] generarDistribucionPoisson() {
        double[] resultado;
        GeneradorDeDistribuciones g = new GeneradorDeDistribuciones();

        double vector[] = new double[muestra];
        for (int i = 0; i < muestra; i++) {
            vector[i] = g.generarPoisson(1 / media);
        }
        resultado = vector;
        return resultado;
    }

    private double[] generarNumerosExponencial() {
        double[] resultado;
        GeneradorDeDistribuciones g = new GeneradorDeDistribuciones();
        resultado = g.generarExponencialNegativa(muestra, media);
        return resultado;
    }
}